package cn.lysj.framework.coupon.service;

import cn.lysj.framework.base.BaseService;
import cn.lysj.framework.common.utils.random.CustomTimeType;
import cn.lysj.framework.common.utils.random.CustomTimeUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.coupon.domain.BirthdayCouponConfig;
import cn.lysj.framework.coupon.domain.Coupon;
import cn.lysj.framework.coupon.repository.BirthdayCouponConfigRepository;
import cn.lysj.framework.coupon.vo.BirthdayCouponToStoreVo;
import cn.lysj.framework.coupon.vo.BirthdayCouponToUserVo;
import cn.lysj.framework.customer.domain.CustomerInfo;
import cn.lysj.framework.customer.domain.CustomerVip;
import cn.lysj.framework.customer.service.CustomerInfoService;
import cn.lysj.framework.customer.service.CustomerVipService;
import cn.lysj.framework.store.domain.Store;
import cn.lysj.framework.store.service.StoreService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zwj
 * @description 生日优惠券配置服务层
 * @create 2019-01-16 09:39:10
 **/
@Slf4j
@Service
@Transactional
public class BirthdayCouponConfigService implements BaseService<BirthdayCouponConfig> {

    @Resource
    private BirthdayCouponConfigRepository birthdayCouponConfigRepository;

    @Resource
    private CustomerInfoService customerInfoService;

    @Resource
    private CustomerVipService customerVipService;

    @Resource
    private CouponService couponService;

    @Resource
    private StoreService storeService;


    @Override
    public BirthdayCouponConfigRepository getRepository() {
        return birthdayCouponConfigRepository;
    }


    /**
     * @author ZhangWenJian
     * @date 2019/1/21 17:29
     * @title addBirthdayCouponConfig
     * @param storeId
     * @param couponIds
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 添加生日配置
     */
    public Resp addBirthdayCouponConfig(String storeId,String[] couponIds){
        List<BirthdayCouponConfig> birthdayCouponConfigs = new ArrayList<>();
        for (String couponId:couponIds){
            Coupon coupon = couponService.getRepository().findByStoreIdAndDelFlagAndId(storeId,1,couponId);
            if(coupon != null){
                BirthdayCouponConfig birthdayCouponConfig = new BirthdayCouponConfig(storeId,couponId);
                birthdayCouponConfigs.add(birthdayCouponConfig);
            }
        }
        birthdayCouponConfigRepository.save(birthdayCouponConfigs);
        return new Resp("操作成功!");
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/21 17:35
     * @title deleteBirthdayCouponConfig
     * @param storeId
     * @param birthdayCouponConfigId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 删除生日的某配置
     */
    public Resp deleteBirthdayCouponConfig(String storeId,String birthdayCouponConfigId){
        BirthdayCouponConfig birthdayCouponConfig = birthdayCouponConfigRepository.findOne(birthdayCouponConfigId);
        if(birthdayCouponConfig == null){
            return new Resp(Resp.PARAM_ERROR,"数据异常!");
        }
        if(!birthdayCouponConfig.getStoreId().equals(storeId)){
            return new Resp(Resp.PARAM_ERROR,"数据异常!");
        }
        birthdayCouponConfigRepository.delete(birthdayCouponConfigId);
        return new Resp("操作成功!");
    }


    /**
     * @author ZhangWenJian
     * @date 2019/1/16 10:20
     * @title storeGetBirthdayCoupon
     * @param storeId
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 获取商户配置的优惠券
     */
    public Resp storeGetBirthdayCoupon(String storeId){
        List<BirthdayCouponToStoreVo> birthdayCouponVo = birthdayCouponConfigRepository.findBirthdayCouponToStoreVo(storeId);
        return new Resp(birthdayCouponVo,"获取商户配置生日券成功!");
    }


    /**
     * @author ZhangWenJian
     * @date 2019/1/18 14:23
     * @title findBirthdayCouponToUserVo
     * @param storeId
     * @return java.util.List<cn.lysj.framework.coupon.vo.BirthdayCouponToUserVo>
     * @description 获取商户可供用户领取的生日券信息
     */
    public List<BirthdayCouponToUserVo> findBirthdayCouponToUserVo(String storeId){
        List<BirthdayCouponToUserVo> birthdayCouponToUserVos = birthdayCouponConfigRepository.findBirthdayCouponToUserVo(storeId);
        for(int i = birthdayCouponToUserVos.size()-1 ; i >= 0 ;i--){
            if(birthdayCouponToUserVos.get(i).getShareType() == 2 && birthdayCouponToUserVos.get(i).getShare() == 0){
                birthdayCouponToUserVos.remove(i);
            }
        }
        return birthdayCouponToUserVos;
    }

    /**
     * @author ZhangWenJian
     * @date 2019/1/18 16:02
     * @title receiveBirthdayCoupon
     * @param customerVipId
     * @param type 1.领取 2.不领取
     * @return cn.lysj.framework.common.utils.web.Resp
     * @description 用户领取生日红包
     */
    public Resp receiveBirthdayCoupon(String customerVipId,Integer type){
        CustomerVip customerVip = customerVipService.findOne(customerVipId);
        if(customerVip == null){
            return new Resp(Resp.PARAM_ERROR,"数据异常!");
        }
        if(CustomTimeUtil.getSysYear().equals(customerVip.getBirthdayGetCouponRecord())){
            return new Resp(Resp.PARAM_ERROR,"无法领取,您已经领取过生日券了!");
        }
        //如果用户同意领红包进行领红包操作
        if(type == 1){
            CustomerInfo customerInfo = customerInfoService.findOne(customerVip.getCustomerInfoId());
            if(customerInfo == null){
                return new Resp(Resp.PARAM_ERROR,"数据异常!");
            }
            Store store = storeService.getRepository().findOne(customerVip.getStoreId());
            if(store == null){
                return new Resp(Resp.PARAM_ERROR,"数据异常!");
            }
            if(store.getAdvanceProvideTime() != null){
                Date beginTime = CustomTimeUtil.changeBirthday(CustomTimeUtil.getSkewingTimeInDay(new Date(),0, CustomTimeType.BEGIN),customerInfo.getBirthday());
                Date endTime = CustomTimeUtil.getSkewingTimeInDay(beginTime,store.getAdvanceProvideTime(), CustomTimeType.END);
                if(customerInfo.getBirthday().compareTo(beginTime)>=0 && customerInfo.getBirthday().compareTo(endTime) <= 0){
                    List<BirthdayCouponToUserVo> birthdayCouponToUserVos = findBirthdayCouponToUserVo(customerVip.getStoreId());
                    for (BirthdayCouponToUserVo birthdayCouponToUserVo:birthdayCouponToUserVos){
                        couponService.customerVipGetCoupon(customerVipId,birthdayCouponToUserVo.getCouponId(),false);
                    }
                }
                else{
                    return new Resp(Resp.PARAM_ERROR,"不满足领取条件");
                }
            }
            else{
                return new Resp(Resp.PARAM_ERROR,"不满足领取条件");
            }
        }
        customerVip.setBirthdayGetCouponRecord(CustomTimeUtil.getSysYear());
        customerVipService.save(customerVip);
        return new Resp("操作成功!");
    }

}
